package com.reggie.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.common.R;
import com.reggie.pojo.Employee;
import com.reggie.service.IEmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest req){
        String password = employee.getPassword();
        //md5解析 密码
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        String userName = employee.getUsername();
        LambdaQueryWrapper<Employee> eqw = new LambdaQueryWrapper<>();
        eqw.eq(Employee::getUsername,userName);
        Employee emp = employeeService.getOne(eqw);

        // username 失败 密码 失败 禁用 失败---成功
        if (emp == null){
            return R.error("用户名不存在,请重新输入！");
        }
        if (!emp.getPassword().equals(password)){
            return R.error("密码不正确,请重新输入！");
        }
        if (emp.getStatus() == 0){
            return R.error("您已被禁用！请联系餐厅老板");
        }
        req.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest req){
        req.getSession().removeAttribute("employee");
        return  R.success("退出成功");
    }

    @PostMapping
    public R<String> save(HttpServletRequest req,@RequestBody Employee employee){
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        /*employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        //获取当前登录的用户id
        Long empId = (Long) req.getSession().getAttribute("employee");
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);*/

        employeeService.save(employee);
        return R.success("新增员工成功");
    }

    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name){
        //分页查询
        Page pageBean = new Page(page,pageSize);

        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //模糊查询
        lqw.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //排序
        lqw.orderByDesc(Employee::getUpdateTime);
        //获取分页数据
        employeeService.page(pageBean,lqw);
        return R.success(pageBean);
    }

    @PutMapping
    public R<String> update(HttpServletRequest req,@RequestBody Employee employee){
        /*Long empId = (Long) req.getSession().getAttribute("employee");

        employee.setUpdateUser(empId);
        employee.setUpdateTime(LocalDateTime.now());*/

        long id = Thread.currentThread().getId();
        log.info("线程id为：{}",id);

        employeeService.updateById(employee);

        return R.success("员工信息修改成功！");
    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){

        Employee employee = employeeService.getById(id);
        if (employee != null){
            return R.success(employee);
        }
        return R.error("没有查询到该员工信息");
    }


}
